package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.config.redssion.RedissonConfig;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.Asserts;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private CategoryFeignClient categoryFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 根据专辑Id获取专辑详情
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getAlbumInfo(Long albumId) {
        Map<String, Object> map = new HashMap<>();
// 未批量导入 布隆过滤器 临时注释，防止测试查询不到数据
//        //  获取布隆过滤器
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
//        //判断过滤器数据中 是否包含 albumId 没有包含直接返回空map
//        if (!bloomFilter.contains(albumId)) {
//            return map;
//        }

        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> albumInfo = albumInfoFeignClient.getAlbumInfo(albumId);
            Asserts.notNull(albumInfo, "专辑列表不存在");
            AlbumInfo albumInfoData = albumInfo.getData();
            Asserts.notNull(albumInfoData, "专辑不存在");
            map.put("albumInfo", albumInfoData);
            return albumInfoData;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfoData -> {
            Result<BaseCategoryView> categoryView = categoryFeignClient.getCategoryView(albumInfoData.getCategory3Id());
            Asserts.notNull(categoryView, "专辑分类信息不存在");
            BaseCategoryView categoryViewData = categoryView.getData();
            Asserts.notNull(categoryViewData, "专辑分类信息不存在");
            map.put("baseCategoryView", categoryViewData);
        }, threadPoolExecutor);

        CompletableFuture<Void> userInfoVoCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfoData -> {
            Result<UserInfoVo> userInfoVo = userInfoFeignClient.getUserInfoVo(albumInfoData.getUserId());
            Asserts.notNull(userInfoVo, "用户列表不存在");
            UserInfoVo userInfoVoData = userInfoVo.getData();
            Asserts.notNull(userInfoVoData, "用户不存在");
            map.put("announcer", userInfoVoData);
        }, threadPoolExecutor);


        CompletableFuture<Void> albumStatInfoCompletableFuture = CompletableFuture.runAsync(() -> {
            Result<AlbumStatVo> albumStatInfo = albumInfoFeignClient.getAlbumStartInfo(albumId);
            Asserts.notNull(albumStatInfo, "专辑统计信息不存在");
            AlbumStatVo albumStatInfoData = albumStatInfo.getData();
            Asserts.notNull(albumStatInfoData, "专辑统计信息不存在");
            map.put("albumStatVo", albumStatInfoData);
        });

        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                categoryViewCompletableFuture,
                userInfoVoCompletableFuture,
                albumStatInfoCompletableFuture
        ).join();


        return map;
    }
}
